//=============================================================================
// main.js
//=============================================================================
function loadScript(url) {
  return new Promise((resolve, reject) => {
    let script = document.createElement("script");
    script.type = "text/javascript";
    script.src = url;
    script._url = url;
    script.async = false;
    script.onload = () => resolve(script);
    script.onerror = () => reject(new Error(`Script load error for ${url}`));
    document.body.appendChild(script);
  });
}

function adjustWindowSize() {
  if (Utils.isNwjs()) {
    const desiredWidth = 1280;
    const desiredHeight = 720;
    const currentWidth = window.innerWidth;
    const currentHeight = window.innerHeight;

    const widthDiff = desiredWidth - currentWidth;
    const heightDiff = desiredHeight - currentHeight;

    const gui = require("nw.gui");
    const win = gui.Window.get();
    win.resizeBy(widthDiff, heightDiff);
  }
}

function launchApp() {
  PluginManager.setup($plugins);

  window.onload = function () {
    adjustWindowSize();
    SceneManager.run(Scene_Boot);
  };
}

async function loadMainScript() {
  const isHant = window.$gameGlobalManager.isHant();
  const pluginsUrl = isHant ? "zh_hant_sources/js/plugins.js" : "js/plugins.js";

  await loadScript(pluginsUrl);
}

async function main() {
  await loadMainScript();
  launchApp();
}

main();
